package cn.edu.zafu.bigdata.lekang.shiyan7;

import java.io.*;
import java.util.*;

public class StudentInfoSystem {
    private static final String FILE_NAME = "C:\\Users\\Administrator\\IdeaProjects\\LearnJava\\src\\main\\java\\cn\\edu\\zafu\\bigdata\\lekang\\shiyan7\\students.dat";
    private Map<String, Student> studentMap;
    private Scanner scanner;

    public StudentInfoSystem() {
        studentMap = new HashMap<>();
        scanner = new Scanner(System.in);
        loadStudents();
    }

    public void start() {
        while (true) {
            System.out.println("======= 学生信息管理系统 =======");
            System.out.println("1. 添加学生信息");
            System.out.println("2. 检索学生信息");
            System.out.println("3. 更新学生信息");
            System.out.println("4. 删除学生信息");
            System.out.println("5. 退出程序");
            System.out.print("请输入操作编号 (1-5): ");
            int choice = scanner.nextInt();
            scanner.nextLine(); // 消耗换行符

            switch (choice) {
                case 1 -> addStudent();
                case 2 -> findStudent();
                case 3 -> updateStudent();
                case 4 -> deleteStudent();
                case 5 -> {
                    saveStudents();
                    System.out.println("退出程序。");
                    return;
                }
                default -> System.out.println("无效的输入，请重新输入。");
            }
        }
    }

    private void addStudent() {
        System.out.print("请输入学号: ");
        String id = scanner.nextLine();
        if (studentMap.containsKey(id)) {
            System.out.println("学号重复，请重新输入。");
            return;
        }
        System.out.print("请输入姓名: ");
        String name = scanner.nextLine();
        System.out.print("请输入电话: ");
        String phone = scanner.nextLine();

        Student student = new Student(id, name, phone);
        studentMap.put(id, student);
        saveStudents();
        System.out.println("学生信息添加成功。");
    }

    private void findStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = studentMap.get(id);
            if (student != null) {
                System.out.println("学生信息: " + student);
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void updateStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            Student student = studentMap.get(id);
            if (student != null) {
                System.out.print("请输入新的姓名: ");
                String name = scanner.nextLine();
                System.out.print("请输入新的电话: ");
                String phone = scanner.nextLine();
                student.setName(name);
                student.setPhone(phone);
                saveStudents();
                System.out.println("学生信息更新成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void deleteStudent() {
        while (true) {
            System.out.print("请输入学号 (输入'QUIT'退出): ");
            String id = scanner.nextLine();
            if ("QUIT".equalsIgnoreCase(id)) {
                break;
            }
            if (studentMap.remove(id) != null) {
                saveStudents();
                System.out.println("学生信息删除成功。");
            } else {
                System.out.println("未找到该学生。");
            }
        }
    }

    private void loadStudents() {
        try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(FILE_NAME))) {
            studentMap = (Map<String, Student>) ois.readObject();
        } catch (IOException | ClassNotFoundException e) {
            System.out.println("加载学生信息失败，使用空数据。");
        }
    }

    private void saveStudents() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))) {
            oos.writeObject(studentMap);
        } catch (IOException e) {
            System.out.println("保存学生信息失败。");
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        StudentInfoSystem system = new StudentInfoSystem();
        system.start();
    }
}

class Student implements Serializable {
    private String id;
    private String name;
    private String phone;

    public Student(String id, String name, String phone) {
        this.id = id;
        this.name = name;
        this.phone = phone;
    }

    // Getters and setters
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}
